require(['loading'], function(loading) {
	var signIn = new Vue({
        el: '#signIn',
        data: {
            showLoading: false,
            yzmUrl: wxUrl + '/v3.3/personalInfo/sendVerifyCode',
            zcUrl: wxUrl + '/v3.3/personalInfo/initMobile',
           
            yzmitem: {
                params:{
                    token: token,
                    mobile: '',
                    type:1
                }
            },
            zcitem: {
                params:{
                    token: token,
                    mobile: '',
            		verifyCode:'',
            		userToken:userToken
                }
            },
            
        },
        ready: function() {
            bindMobileKeyUp();
            bindYzmClick();
            bindSubmit();
        },
        methods: {
            getyzm: function() {
                this.$http.get(this.yzmUrl, this.yzmitem)
                    .then(function(response) {
                        var result = JSON.parse(response.body);
                        if(result.data!=null){
                            var success = result.data.success;
                            console.log(success)
                           
                        }
                    },function(response) {
                        //errorCallback
                    });
            },
            toRegister: function() {
                this.$http.get(this.zcUrl, this.zcitem)
                    .then(function(response) {
                        var result = JSON.parse(response.body);
                        if(result.code != null && result.code == 0){
                            if(result.data!=null){
                                var success = result.data.success;
                                var userToken = result.data.userToken;
                                if(success){
                                    localStorage.setItem('_userToken',userToken);
                                    promptBox("注册成功");
                                    setTimeout(function(){
                                        window.location.href = self_base_path + "/sales/sales_index.html";
                                    },1000)
                                }else{
                                    promptBox("注册失败，重新来吧");
                                }
                            }
                        }
                        
                    },function(response) {
                        //errorCallback
                    });
            }
        }
    });
	
    function bindMobileKeyUp(){
    	$('.content').on('keyup',function(){
            $('.clear').show();
        });
        $('.clear').click(function(){
            $(this).hide();
            $('.content').val('');
        });
    }
	//获取短信验证码
    function bindYzmClick(){
        var validCode=true;
        $(".msgs").click(function() {
        	if(!checkPhone()){
        		return;
        	}
        	var phone = $("#tel").val();
        	signIn.yzmitem.params.mobile=phone;
            var time=60;
            var code=$(this);
            if (validCode) {
                validCode=false;
                code.addClass("msgs1");
                signIn.getyzm();
                var t=setInterval(function  () {
                    time--;
                    code.html(time+"s后重发");
                    if (time==0) {
                        clearInterval(t);
						code.html("重发验证码")
						code.css("color","#FF9500");
                        validCode=true;
                        code.removeClass("msgs1");
                    }
                },1000)
            }
        })
    }

    function checkPhone(){
    	var phone = $("#tel").val();
    	if(phone == "") {
			promptBox("手机号码不能为空");
		} else {
			var re = /^[1][34587]\d{9}$/; //手机号码验证正则表达式
			if(re.test(phone)) {
				return true;
			} else {
				promptBox("请输入正确的手机号码");
			}
		}
		return false;
    }

    function bindSubmit(){
    	$("#submit").click(function() {
			if(checkPhone()){
				var yzm = $("#yz").val();
				if(yzm == ''){
					promptBox("请输入验证码");
					return;
				}
				signIn.zcitem.params.verifyCode=yzm;
				var phone = $("#tel").val();
        		signIn.zcitem.params.mobile=phone;
				signIn.toRegister();
			}
		});
    }
});
